package net.xprogrammer.booclipse.generator.toc.parser.daisy;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import net.xprogrammer.booclipse.generator.toc.parser.Parser;
import net.xprogrammer.booclipse.generator.toc.parser.daisy.model.Content;
import net.xprogrammer.booclipse.generator.toc.parser.daisy.model.DocTitle;
import net.xprogrammer.booclipse.generator.toc.parser.daisy.model.Head;
import net.xprogrammer.booclipse.generator.toc.parser.daisy.model.Meta;
import net.xprogrammer.booclipse.generator.toc.parser.daisy.model.NavLabel;
import net.xprogrammer.booclipse.generator.toc.parser.daisy.model.NavMap;
import net.xprogrammer.booclipse.generator.toc.parser.daisy.model.NavPoint;
import net.xprogrammer.booclipse.generator.toc.parser.daisy.model.Ncx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.thoughtworks.xstream.XStream;

/**
 * daisy格式的toc解析器，oreilly官方发布的epub格式书籍采用这个解析器
 * 
 * @author gyzhang
 * @date 2012-4-1 下午5:53:11
 * @version 3.0
 */
public class DaisyTocParser implements Parser {

	private static final Logger	log	= LoggerFactory.getLogger(DaisyTocParser.class);

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File in = new File("d:" + File.separator + "toc.ncx");
		File out = new File("d:" + File.separator + "booclipse.xml");
		String hrefPrefix = "html/booclipse/web/cssTDG3/";//存放html文件的目录

		DaisyTocParser parser = new DaisyTocParser();
		try {
			parser.writeTOC(in, out, hrefPrefix);
		} catch (IOException e) {
			log.error(e.getMessage());
		}

	}

	/**
	 * 将给定的daisy toc.ncx文件解析输出eclipse help toc格式的文件
	 * 
	 * @param in
	 *            daisy toc文件，一般为toc.ncx
	 * @param out
	 *            解析输出eclipse help toc格式的文件
	 * @param hrefPrefix
	 *            eclipse中插件项目存放html文件的目录
	 * @throws IOException
	 */
	public void writeTOC(File in, File out, String hrefPrefix) throws IOException {
		DaisyTocParser parser = new DaisyTocParser();
		Ncx ncx = null;
		FileWriter fileWriter = null;

		try {
			ncx = parser.parse(in);
			List<NavPoint> navPoints = ncx.getNavMap().getNavPoints();

			fileWriter = new FileWriter(out);

			if (null != navPoints) {
				for (NavPoint navPoint : navPoints) {
					navPoint.write2Booclipse(navPoint, hrefPrefix, fileWriter, 0);
				}
			}

		} finally {
			if (null != fileWriter) {
				fileWriter.close();
			}
		}
	}

	@SuppressWarnings("unchecked")
	public Ncx parse(File file) throws IOException {

		Ncx result = null;
		InputStream input = null;
		input = new FileInputStream(file);

		XStream xstream = new XStream();
		//取别名
		xstream.processAnnotations(Ncx.class);
		xstream.processAnnotations(Head.class);
		xstream.processAnnotations(Meta.class);
		xstream.processAnnotations(DocTitle.class);
		xstream.processAnnotations(NavMap.class);
		xstream.processAnnotations(NavPoint.class);
		xstream.processAnnotations(NavLabel.class);
		xstream.processAnnotations(Content.class);
		//去掉标签
		xstream.addImplicitCollection(Head.class, "metas");
		xstream.addImplicitCollection(NavMap.class, "navPoints");
		xstream.addImplicitCollection(NavPoint.class, "navPoints");

		try {
			result = (Ncx) xstream.fromXML(input);
		} finally {
			try {
				input.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return result;
	}

}
